package com.linln.modules.system.service.impl;

import com.linln.common.data.QuerySpec;
import com.linln.modules.system.domain.Dept;
import com.linln.modules.system.domain.Order;
import com.linln.modules.system.domain.Porter;
import com.linln.modules.system.repository.OrderRepository;
import com.linln.modules.system.repository.PorterRepository;
import com.linln.modules.system.service.PorterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;


@Service
public class PorterServiceImpl implements PorterService {
    @Autowired
    private PorterRepository porterRepository;

    @Autowired
    private OrderRepository orderRepository;

    /**
     * 更新护工限制字段
     * @param depId  科室id 下单时候传
     * @param id  护工id
     * @param numberLimit 下单时候项目限制
     * @param orderId 退单时候传
     * @param flag  1.下单 2.结束订单单
     */
    @Override
    public void updatePorter(long depId,long id,int numberLimit,long orderId,int flag) {
        int serviceNumber = 0;//护工当前服务人数
        int leastLimit = 0;//最少人数限制
        int isStatus = 1;//护工当前否可以继续接收服务 0否 1是
        //查询护工
        Porter porter = porterRepository.findById(id).get();
        serviceNumber = porter.getServiceNumber();
        leastLimit = porter.getLeastLimit();
        isStatus = porter.getIsStatus();
        //查询护工的订单
        QuerySpec querySpec = QuerySpec.matching();
        List<Object> statusList = new LinkedList<>();
        statusList.add(0);
        statusList.add(1);
        statusList.add(2);
        statusList.add(3);
        statusList.add(4);
        // 1.下单 2.结束订单
        if(flag == 1){
            //查询历史订单
            querySpec.withMatcherValue("type", QuerySpec.EQUAL,"1")
                    .withMatcherValue("status", QuerySpec.EQUAL,"1")
                    .withMatcherValue("porterId", QuerySpec.EQUAL,String.valueOf(id))
                    .withMatcherIn("orderStatus",statusList);
            List<Order> orderList = orderRepository.findAll(QuerySpec.of(querySpec));
            if (orderList.size()>0) {
                //定义最小限制数
                int min = 0;
                for (Order order : orderList) {
                    //所有历史订单限制人数
                    int numberLimit1 = order.getNumberLimit();
                    if (min==0){
                        min = Math.min(numberLimit1, numberLimit);
                    }else{
                        if (numberLimit1<=numberLimit){
                            if (numberLimit1<=min){
                                min = numberLimit1;
                            }
                        }else{
                            if (numberLimit<=min){
                                min = numberLimit;
                            }
                        }
                    }
                }
                serviceNumber++;
                leastLimit = min;
                if (serviceNumber>=leastLimit){
                    serviceNumber = leastLimit;
                    isStatus = 0;
                }else{
                    isStatus = 1;
                }
            }else{
                serviceNumber = 1;
                leastLimit = numberLimit;
                //如果限制人数小于等于1  则不可以接单了
                if (numberLimit<=1){
                    isStatus = 0;
                }else {
                    isStatus = 1;
                }
                porter.setDepId(depId);
            }
        }else{
            querySpec.withMatcherValue("id", QuerySpec.NOT_EQUAL,String.valueOf(orderId))
                    .withMatcherValue("type", QuerySpec.EQUAL,"1")
                    .withMatcherValue("status", QuerySpec.EQUAL,"1")
                    .withMatcherValue("porterId", QuerySpec.EQUAL,String.valueOf(id))
                    .withMatcherIn("orderStatus",statusList);
            List<Order> orderList = orderRepository.findAll(QuerySpec.of(querySpec));
            if (orderList.size()>0) {
                //定义最小限制数
                int min = 0;
                for (Order order : orderList) {
                    //所有历史订单限制人数
                    int numberLimit1 = order.getNumberLimit();
                    if (min==0){
                        min = numberLimit1;
                    }else{
                        if (numberLimit1<=min){
                            min = numberLimit1;
                        }
                    }
                }
                if (serviceNumber>=1){
                    serviceNumber--;
                }else{
                    serviceNumber = 0;
                }
                leastLimit = min;
                if (serviceNumber>=leastLimit){
                    isStatus = 0;
                }else{
                    isStatus = 1;
                }
            }else{
                serviceNumber = 0;
                leastLimit = 0;
                isStatus = 1;
            }
            if (serviceNumber==0){
                porter.setDepId(0L);
            }
        }
        porter.setServiceNumber(serviceNumber);
        porter.setLeastLimit(leastLimit);
        porter.setIsStatus((byte) isStatus);
        porterRepository.save(porter);
    }
}
